class_name PlayerStateChecker
extends Node

var is_on_ground: bool = false

var is_ground_detected: bool = false

var is_head_collided: bool = false

var is_up_ladder_detected: bool = false

var is_down_ladder_detected: bool = false

@onready var player: Player = $"../"

@onready var head_collision: CollisionShape2D = %HeadCollision

@onready var body_collision: CollisionShape2D = %BodyCollision

@onready var ground_checker: Area2D = %GroundChecker

@onready var head_colliding_checker: RayCast2D = %HeadCollidingChecker

@onready var up_ladder_checker: Area2D = %UpLadderChecker

@onready var down_ladder_checker: Area2D = %DownLadderChecker


func on_update(_delta: float) -> void:
	is_on_ground = player.is_on_floor()
	is_ground_detected = ground_checker.get_overlapping_bodies().size() > 0
	is_up_ladder_detected = up_ladder_checker.get_overlapping_bodies().size() > 0
	is_down_ladder_detected = down_ladder_checker.get_overlapping_bodies().size() > 0
	is_head_collided = head_colliding_checker.is_colliding()
	
	#region 处理单向平台下降
	# 此处的 3 是自己设定的单向平台的碰撞层
	# 当双击 crouch 时，玩家忽略单向平台的碰撞层，从而可以下降
	var mask = 1 << 3
	if player.input_handler.is_action_double_click_started("crouch"):
		player.collision_mask &= ~mask
		get_tree().create_timer(0.3).timeout.connect(func(): player.collision_mask |= mask)
	#endregion


func change_to_stand_collision() -> void:
	head_collision.disabled = false
	body_collision.disabled = false


func change_to_crouch_collision() -> void:
	head_collision.disabled = true
	body_collision.disabled = false


func _process(delta: float) -> void:
	on_update(delta)
